package com.carleasoft.mps.data.redis.impl;


import com.carleasoft.mps.data.redis.IRedisDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.*;

/**
 * redis连接与回收实例
 * @author: haoyi
 */
@Service("redisDataSource")
public class RedisDataSourceImpl implements IRedisDataSource {
    private  Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JedisPool jedisPool;

    /**
     * 创建实例
     */
    @Override
    public Jedis getRedisClient() {
        try {
            Jedis jedis = jedisPool.getResource();

            return jedis;
        } catch (Exception e) {
            log.error("getRedisClent error", e);
        }
        return null;
    }

    /**
     * 回收实例
     */
    @Override
    public void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}
